package com.base.cn.platform.os.controller.order.count;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.manage.subject.SysSubject;
import com.base.cn.platform.os.entity.order.count.*;
import com.base.cn.platform.os.service.manage.subject.SysSubjectBiz;
import com.base.cn.platform.os.service.order.order.OrderBiz;
import com.base.cn.platform.os.service.order.order.OrderDetailsBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单统计Controller接口
 *
 * @author s.li
 * @create 2018-08-23-18:48
 */
@RestController
@RequestMapping("/manage/order")
public class OrderCountController extends BaseController {

    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private OrderDetailsBiz orderDetailsBiz;
    @Autowired
    private SysSubjectBiz sysSubjectBiz;

    /**
     * 获取当天订单量
     * @param dateTime 时间
     * @return 统计数据对象
     */
    @RequestMapping("/queryOrderNowDayNum")
    public List<OrderDayNum> queryOrderNowDayNum(@RequestParam("dateTime") String dateTime){
        return orderBiz.queryOrderNowDayNum(dateTime);
    }

    /**
     * 查询购买课程的用户量
     * @return 用户量
     */
    @RequestMapping("/queryBuyCourseUserNum")
    public Integer queryBuyCourseUserNum(){
        return orderBiz.queryBuyCourseUserNum();
    }

    /**
     * 查询订单总数
     * @return 订单总数量
     */
    @RequestMapping("/queryOrderCountNum")
    public Integer queryOrderCountNum(){
        return orderBiz.count("1=1").intValue();
    }

    /**
     * 查询订单流水总金额
     * @return 订单流水总金额
     */
    @RequestMapping("/queryOrderSumPrice")
    public double queryOrderSumPrice(){
        return orderBiz.queryOrderSumPrice();
    }

    /**
     * 统计专业的销售额度的对比数据
     * @return List<OrderSubjectNum>
     */
    @RequestMapping("/orderSubjectNumList")
    public List<OrderSubjectNum> queryOrderSubjectPriceCountNum(){
        List<OrderSubjectNum> orderSubjectNumList = orderDetailsBiz.queryOrderSubjectPriceCountNum();
        if(ObjectUtils.isNotEmpty(orderSubjectNumList)){
            String subjectIds = orderSubjectNumList.stream().filter(e->StringUtils.isNotEmpty(e.getSubjectIds())).map(e-> StringUtils.subHeadTailString(e.getSubjectIds(),",")).collect(Collectors.joining(","));
            SysSubject sysSubject = new SysSubject();
            sysSubject.setQueryIds(subjectIds);
            Map<BigDecimal,SysSubject> subjectMaps = sysSubjectBiz.findSubjectToIdMap(sysSubject);
            if(ObjectUtils.isNotEmpty(subjectMaps)){
                orderSubjectNumList.forEach(orderSubjectNum -> {
                    String _subjectIds = orderSubjectNum.getSubjectIds();
                    _subjectIds = StringUtils.subHeadTailString(_subjectIds,",");
                    if(StringUtils.isNotEmpty(_subjectIds)){
                        String[] arr = _subjectIds.split(",");
                        List<Map<String,Object>> subjectList = new ArrayList<>();
                        Arrays.stream(arr).forEach(subjectId ->{
                            SysSubject subject = subjectMaps.get(new BigDecimal(subjectId));
                            Map<String,Object> _subject = new HashMap<>();
                            _subject.put("id",subject.getId());
                            _subject.put("depict",subject.getSubjectName());
                            _subject.put("parentId",subject.getParentId());
                            _subject.put("childSubjectList",subject.getChildSubjectList());
                            subjectList.add(_subject);
                        });
                        orderSubjectNum.setSubjectList(subjectList);
                    }
                });
            }
        }
        return orderSubjectNumList;
    }

    /**
     * 查询统计订单价格区域的数据
     * @return OrderPriceCountNum
     */
    @RequestMapping("/queryOrderPriceCountNum")
    public OrderPriceCountNum queryOrderPriceCountNum(){
        return orderBiz.queryOrderPriceCountNum();
    }

    /**
     * 查询订单支付方式占比统计数据
     * @return PayTypeCountNum
     */
    @RequestMapping("/queryOrderPayTypeCountNum")
    public OrderPayTypeCountNum queryOrderPayTypeCountNum(){
        return orderBiz.queryOrderPayTypeCountNum();
    }

    /**
     * 查询订单的来源数据统计
     * @return OrderFormCountNum
     */
    @RequestMapping("/queryOrderFormCountNum")
    public OrderFormCountNum queryOrderFormCountNum(){
        return orderBiz.queryOrderFormCountNum();
    }

    /**
     * 获取某段时间的订单曲线
     * @param map 时间
     * @return 统计数据对象
     */
    @RequestMapping("/queryOrderDayNum")
    public List<Map<String,Object>> queryOrderDayNum(@RequestBody Map map){
        return orderBiz.queryOrderDayNum(map);
    }
}
